﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Google.GData.Photos;
using Google.GData.Extensions.Location;
using Google.GData.Extensions;
using Google.GData.Client;
using TuoiThoProject.DAL;
using System.IO;

namespace TuoiThoProject.BLL
{
    public class PhotoBLL
    {
        NewsDataContext data = new NewsDataContext();

        private PicasaService service;
        public PicasaService Service
        {
            get
            {
                if (service == null)
                {
                    service = new PicasaService("Tuoi-tho");
                }
                return service;
            }
            set
            {
                service = value;
            }
        }



        public List<PhotoEntity> getImage_ByCategoryId(string CategoryId, string GmailId)
        {
            tblPicasa picasaUser = data.tblPicasas.SingleOrDefault(p => p.cGmailId == GmailId);
            List<PhotoEntity> lstPhoto = new List<PhotoEntity>();

            if (picasaUser != null)
            {
                this.Service.setUserCredentials(picasaUser.cGmailId, picasaUser.cPassword);

                PhotoQuery query = new PhotoQuery(PicasaQuery.CreatePicasaUri(picasaUser.cGmailId, CategoryId));

                PicasaFeed feed = this.Service.Query(query);

                foreach (PicasaEntry entry in feed.Entries)
                {
                    PhotoEntity pe = new PhotoEntity();

                    pe.Title = entry.Title.Text;
                    pe.Summary = entry.Summary.Text;

                    PhotoAccessor ac = new PhotoAccessor(entry);
                    pe.AlbumId = ac.AlbumId;
                    pe.PhotoId = ac.Id;
                    pe.Camera = "";
                    if (entry.Exif.Make != null && entry.Exif.Model != null)
                    {
                        pe.Camera = String.Format("{0} {1}", entry.Exif.Make.Value, entry.Exif.Model.Value);
                    }
                    pe.ContentUrl = entry.Media.Content.Attributes["url"] as string;

                    pe.ThumbnailUrl_0 = entry.Media.Thumbnails[0].Attributes["url"] as string;
                    pe.ThumbnailUrl_1 = entry.Media.Thumbnails[1].Attributes["url"] as string;
                    pe.ThumbnailUrl_2 = entry.Media.Thumbnails[2].Attributes["url"] as string;

                    lstPhoto.Add(pe);
                }
            }



            return lstPhoto;
        }

        public List<PhotoEntity> getImage_RecentUpload(int NumberPhoto, string GmailId)
        {
            tblPicasa picasaUser = data.tblPicasas.SingleOrDefault(p => p.cGmailId == GmailId);

            List<PhotoEntity> lstPhoto = new List<PhotoEntity>();

            if (picasaUser != null)
            {
                this.Service.setUserCredentials(picasaUser.cGmailId, picasaUser.cPassword);

                PhotoQuery query = new PhotoQuery(PicasaQuery.CreatePicasaUri(picasaUser.cGmailId));
                query.NumberToRetrieve = NumberPhoto;

                PicasaFeed feed = this.Service.Query(query);

                foreach (PicasaEntry entry in feed.Entries)
                {
                    PhotoEntity pe = new PhotoEntity();

                    pe.Title = entry.Title.Text;
                    pe.Summary = entry.Summary.Text;

                    PhotoAccessor ac = new PhotoAccessor(entry);
                    pe.AlbumId = ac.AlbumId;
                    pe.PhotoId = ac.Id;
                    pe.Camera = "";
                    if (entry.Exif.Make != null && entry.Exif.Model != null)
                    {
                        pe.Camera = String.Format("{0} {1}", entry.Exif.Make.Value, entry.Exif.Model.Value);
                    }
                    pe.ContentUrl = entry.Media.Content.Attributes["url"] as string;

                    pe.ThumbnailUrl_0 = entry.Media.Thumbnails[0].Attributes["url"] as string;
                    pe.ThumbnailUrl_1 = entry.Media.Thumbnails[1].Attributes["url"] as string;
                    pe.ThumbnailUrl_2 = entry.Media.Thumbnails[2].Attributes["url"] as string;

                    lstPhoto.Add(pe);
                }
            }

            return lstPhoto;
        }

        public List<PhotoEntity> getImage_All(string GmailId)
        {
            tblPicasa picasaUser = data.tblPicasas.SingleOrDefault(p => p.cGmailId == GmailId);
            List<PhotoEntity> lstPhoto = new List<PhotoEntity>();
            if (picasaUser != null)
            {
                this.Service.setUserCredentials(picasaUser.cGmailId, picasaUser.cPassword);

                PhotoQuery query = new PhotoQuery(PicasaQuery.CreatePicasaUri(picasaUser.cGmailId));

                PicasaFeed feed = this.Service.Query(query);

                foreach (PicasaEntry entry in feed.Entries)
                {
                    PhotoEntity pe = new PhotoEntity();

                    pe.Title = entry.Title.Text;
                    pe.Summary = entry.Summary.Text;

                    PhotoAccessor ac = new PhotoAccessor(entry);
                    pe.AlbumId = ac.AlbumId;
                    pe.PhotoId = ac.Id;
                    pe.Camera = "";
                    if (entry.Exif.Make != null && entry.Exif.Model != null)
                    {
                        pe.Camera = String.Format("{0} {1}", entry.Exif.Make.Value, entry.Exif.Model.Value);
                    }
                    pe.ContentUrl = entry.Media.Content.Attributes["url"] as string;

                    pe.ThumbnailUrl_0 = entry.Media.Thumbnails[0].Attributes["url"] as string;
                    pe.ThumbnailUrl_1 = entry.Media.Thumbnails[1].Attributes["url"] as string;
                    pe.ThumbnailUrl_2 = entry.Media.Thumbnails[2].Attributes["url"] as string;

                    lstPhoto.Add(pe);
                }


            }

            return lstPhoto;
        }

        public void PostPhoto(string file, string AlbumId, string GmailId)
        {
            tblPicasa picasaUser = data.tblPicasas.SingleOrDefault(p => p.cGmailId == GmailId);

            if (picasaUser != null)
            {
                Uri postUri = new Uri(PicasaQuery.CreatePicasaUri(picasaUser.cGmailId, AlbumId));

                FileInfo fileInfo = new FileInfo(file);
                FileStream fileStream = fileInfo.OpenRead();

                if (String.IsNullOrEmpty(AlbumId))
                {
                    PicasaEntry entry = (PicasaEntry)this.Service.Insert(new Uri("https://picasaweb.google.com/data/feed/api/user/default/albumid/default"), fileStream, "image/jpeg", file);
                }
                else
                {
                    PicasaEntry entry = (PicasaEntry)this.Service.Insert(postUri, fileStream, "image/jpeg", file);
                }

                fileStream.Close();
            }
        }

        public void UpdatePhoto(string PhotoId, string Title, string Summary, string GmailId)
        {
            tblPicasa picasaUser = data.tblPicasas.SingleOrDefault(p => p.cGmailId == GmailId);

            if (picasaUser != null)
            {
                this.Service.setUserCredentials(picasaUser.cGmailId, picasaUser.cPassword);

                PhotoQuery query = new PhotoQuery(PicasaQuery.CreatePicasaUri(picasaUser.cGmailId));

                PicasaFeed feed = this.Service.Query(query);

                foreach (PicasaEntry entry in feed.Entries)
                {
                    PhotoAccessor ac = new PhotoAccessor(entry);

                    if (ac.Id == PhotoId)
                    {
                        entry.Title.Text = Title;
                        entry.Summary.Text = Summary;

                        PicasaEntry entryUpdated = (PicasaEntry)entry.Update();
                        break;
                    }
                }

            }
        }

        public void DeletePhoto(string PhotoId, string GmailId)
        {
            tblPicasa picasaUser = data.tblPicasas.SingleOrDefault(p => p.cGmailId == GmailId);

            if (picasaUser != null)
            {
                this.Service.setUserCredentials(picasaUser.cGmailId, picasaUser.cPassword);

                PhotoQuery query = new PhotoQuery(PicasaQuery.CreatePicasaUri(picasaUser.cGmailId));

                PicasaFeed feed = this.Service.Query(query);

                foreach (PicasaEntry entry in feed.Entries)
                {
                    PhotoAccessor ac = new PhotoAccessor(entry);

                    if (ac.Id == PhotoId)
                    {
                        entry.Delete();
                        break;
                    }
                }

            }
        }


    }
}